package com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.front.byjhpzadd_frm.voactions;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bff.spi.*;
import com.inspur.edp.bef.api.action.*;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.List;

public class savebeforecheckVOAction extends AbstractFSAction<VoidActionResult> {

	private String sbid;
	private String year;
	private String zt;
	private String id;
	public savebeforecheckVOAction(String sbid, String year, String zt,
			String id) {
		this.sbid = sbid;
		this.year = year;
		this.zt = zt;
		this.id = id;
	}
	@Override
	public void execute() {
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			String byid = "db558bb5-532f-430d-b610-13f9fc5835bd";
			IStandardLcp bylcp = iLcpFactory.createLcpByBEId(byid);
			EntityFilter filter = new EntityFilter();
			List<FilterCondition> builderConditions=FilterCondition.builder()
					.andEqual("year",year)
					.andEqual("sbid",sbid)
					.build();
			filter.addFilterConditions((ArrayList<FilterCondition>) builderConditions);
			List<IEntityData> query = bylcp.query(filter);
			if (zt.equals("add")&&!query.isEmpty()){
				throw new RuntimeException("该年份的设备已添加，请重新添加！");
			}

			if (zt.equals("edit")){
				for (IEntityData iEntityData : query) {
					String orisbid = (String) ((IValuesContainer)iEntityData.getValue("sbid")).getValue("sbid");
					String oriyear = (String) iEntityData.getValue("year");
					String oriid = (String) iEntityData.getID();
					if (!oriid.equals(id)&&orisbid.equals(sbid)&&oriyear.equals(year)){
						throw new RuntimeException("该年份的设备已添加，请重新添加！");
					}
				}
			}
		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}
	}
}
